import qs from 'qs';

// 给页面添加 uni 能力
export function addUniSdk() {
  if ($nuxt.$route.query.fromkey === 'wxapp') {
    const uniScript = document.createElement('script');
    uniScript.src = 'https://www.xnbz.site/static/uni-sdk-weixin.js';
    document.body.appendChild(uniScript);
  }
}

export const navTo = (path) => {
  if (window?.location?.search) {
    const urlparams = qs.parse(window.location.search.substring(1));
    if (urlparams?.username) {
      if (path.includes('?')) {
        path = `${path}&username=${urlparams?.username}`;
      } else {
        path = `${path}?username=${urlparams?.username}`;
      }
    }
  }

  window.location.href = path;
};

// 获取小程序共有的页面地址参数
export function getWxUrlParams() {
  const urlParams = {
    fromkey: 'wxapp',
  };
  return qs.stringify(urlParams);
}
